package com.mobimap.nhom9.bus;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.mobimap.nhom9.dao.MapSQLiteHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class MobimapProvider {
	private Context mContext;
	private SQLiteDatabase database;
	private IDirectionBUS directionBus;
	private ISearchEngine searchEngine;
	private IPlaceBUS placeBus;
	
	private MobimapProvider(Context context)	{
		this.mContext = context;
		MapSQLiteHelper sqliteHelper = new MapSQLiteHelper(this.mContext);
		this.database = sqliteHelper.getReadableDatabase();
		this.createMapData();
		
		this.searchEngine = new DefaultSearchEngine(this.database);
		this.directionBus = new DirectionBUS(this.mContext);
		this.placeBus = new PlaceBUS(this.database);
	}
	/**
	 * Upload Map data if needed
	 */
	private void createMapData() {
		String MAPDATA_LOCAL_ASSET = "datalocal.zip";
		File folder = new File(MAPDATA_LOCAL_FOLDER);
		try {
			if (!folder.exists()) {
				folder.mkdirs();
				final int BUFFER = 2048;
				BufferedOutputStream dest = null;
				InputStream is = mContext.getAssets().open(MAPDATA_LOCAL_ASSET);
				ZipInputStream zis = new ZipInputStream(
						new BufferedInputStream(is));
				ZipEntry entry;

				while ((entry = zis.getNextEntry()) != null) {
					int count;
					byte data[] = new byte[BUFFER];

					FileOutputStream fos = new FileOutputStream(
							MAPDATA_LOCAL_FOLDER + entry.getName());
					dest = new BufferedOutputStream(fos, BUFFER);
					while ((count = zis.read(data, 0, BUFFER)) != -1) {
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
				}
				zis.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	//Static function for GUI
	private static MobimapProvider _mobimapProvider;
	public static void createInstance(Context context)	{
		if (_mobimapProvider == null)	{
			_mobimapProvider = new MobimapProvider(context);
		}
	}
	/**
	 * get default search engine
	 * @param context
	 * @return
	 */
	public static ISearchEngine getSearchEngine(Context context)	{
		createInstance(context);
		return _mobimapProvider.searchEngine;
	}
	/**
	 * get default direction bus
	 * @param context
	 * @return
	 */
	public static IDirectionBUS getDirectionBus(Context context)	{
		createInstance(context);
		return _mobimapProvider.directionBus;
	}
	/**
	 * get place bus
	 * @param context
	 * @return
	 */
	public static IPlaceBUS getPlaceBus(Context context)	{
		createInstance(context);
		return _mobimapProvider.placeBus;
	}
	/**
	 * get app context
	 * @return
	 */
	public static Context getContext()	{
		return _mobimapProvider.mContext;
	}
	//STATIC VALUE
	public static String APP_DATA_FOLDER = "/data/data/com.mobimap.nhom9.gui/";
	public static String MAPDATA_LOCAL_FOLDER = APP_DATA_FOLDER + "mapdata/local/";
}
